/*------------------------------------------------------------------------------
 * COPYRIGHT Ericsson 2015
 *
 * The copyright to the computer program(s) herein is the property of
 * Ericsson Inc. The programs may be used and/or copied only with written
 * permission from Ericsson Inc. or in accordance with the terms and
 * conditions stipulated in the agreement/contract under which the
 * program(s) have been supplied.
 *----------------------------------------------------------------------------*/
package com.ericsson.ngin.oauth.common.cassandra.dao.impl;

import java.io.IOException;

import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.thrift.transport.TTransportException;
import org.cassandraunit.CQLDataLoader;
import org.cassandraunit.dataset.cql.ClassPathCQLDataSet;
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;

@Ignore
public class AbstractCassandraCT {

	protected static final String hostIp = "127.0.0.1";
	protected static final int port = 9142;
	protected static final String defaultKeySpace = "default";
	protected static final String hostAndPort = "127.0.0.1:9142,127.0.0.1:9142";
	protected static final String defualtCassUser = "cassandra";
	protected static final String defualtCassPw = "cassandra";
	protected static final String datacenter = "datacenter1";

	protected static Cluster cluster;
	protected static Session session;
	private static boolean hasInit = false;

	@BeforeClass
	public static void beforeClass() throws Exception {
		initCassandraOnce();
	}

	@AfterClass
	public static void afterClass() throws InterruptedException {
	}

	protected static void initCassandraOnce()
			throws ConfigurationException, TTransportException, IOException, InterruptedException {
		if (hasInit) {
			return;
		}
		EmbeddedCassandraServerHelper.startEmbeddedCassandra(30000);

		cluster = new Cluster.Builder().addContactPoints(hostIp).withPort(port).build();
		session = cluster.connect();
		CQLDataLoader dataLoader = new CQLDataLoader(session);
		dataLoader.load(new ClassPathCQLDataSet("create-table.cql", true, false, defaultKeySpace));
		dataLoader.load(new ClassPathCQLDataSet("data.cql", false, false, defaultKeySpace));
		hasInit = true;
	}

	private static void startSecureCassandra()
			throws ConfigurationException, TTransportException, IOException, InterruptedException {
		EmbeddedCassandraServerHelper.startEmbeddedCassandra("credential-cass.yaml");
		// sleep 10 seconds to wait default user created by cassandra.
		Thread.sleep(10000);

		cluster = new Cluster.Builder().addContactPoints(hostIp).withPort(port)
				.withCredentials(defualtCassUser, defualtCassPw).build();
	}

}
